﻿using System.Xml.Linq;

namespace SchemaGen.Xsd
{
    public abstract class SEntityBase
    {
        protected XName _ElementName { get; private set; }

        public string Name { get; private set; }
        public XName Type { get; set; }
        public SDocumentation Documentation { get; set; }
        public bool IsFixed { get; set; }
        public object DefaultValue { get; set; }

        internal SEntityBase(XName elementName, string name)
        {
            _ElementName = elementName;
            Name = name;
        }

        internal virtual XElement Create()
        {
            var result = new XElement(_ElementName,
                new XAttribute("name", Name));

            if (Type != null) result.Add(new XAttribute("type", Type));
            if (DefaultValue != null)
                result.Add(IsFixed ?
                    new XAttribute("fixed", DefaultValue) :
                    new XAttribute("default", DefaultValue));

            if (Documentation != null && !string.IsNullOrWhiteSpace(Documentation.Comment))
                result.Add(Documentation.Create());

            return result;
        }
    }
}
